package com.fxsh168.server.fragments;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.fxsh168.server.activity.OrderDetailsActivity;
import com.fxsh168.server.adapters.WholeOrderAdapter;
import com.fxsh168.server.base.BaseFragment;
import com.fxsh168.server.entitys.WholeOrderEntity;
import com.fxsh168.server.R;
import com.fxsh168.server.interfaces.Constant;
import com.google.gson.Gson;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

import static android.content.Context.MODE_PRIVATE;
import static com.fxsh168.server.R.id.springview;

public class Ordering extends BaseFragment implements AdapterView.OnItemClickListener {
    private ListView mListViewing;
    private SpringView springView;
    private List<WholeOrderEntity.DataBean> orderingList;
    private WholeOrderAdapter orderingAdapter;
    private String mPhone, token;
    private List<WholeOrderEntity.DataBean> data;
    private WholeOrderEntity wholeOrderEntity;
    TextView teNoOders;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_ordering, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SharedPreferences sp = getActivity().getSharedPreferences(Constant.LOGINCLASS, MODE_PRIVATE);
        mPhone = sp.getString("mPhone", "");
        token = sp.getString("token", "");
        springView = (SpringView) view.findViewById(springview);
        teNoOders = (TextView) view.findViewById(R.id.teNoOrder);
        mListViewing = (ListView) view.findViewById(R.id.listViewing);
        mListViewing.setOnItemClickListener(Ordering.this);
        initSpringview();
        initData();
        orderingList = new ArrayList<>();
        orderingAdapter = new WholeOrderAdapter(Ordering.this.getActivity(), orderingList);
        mListViewing.setAdapter(orderingAdapter);
        //获得数据
        if (!token.isEmpty() && !mPhone.isEmpty()) {
            handlerOrdering.sendEmptyMessage(0);
        }
    }

    private Handler handlerOrdering = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            getData();
            handlerOrdering.sendEmptyMessageDelayed(0, 3000);
        }
    };
    private void initData(){
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }

            @Override
            public void onLoadmore() {

            }
        });
    }
    private void initSpringview() {
        springView.setHeader(new DefaultHeader(getActivity()));
        springView.setGive(SpringView.Give.BOTH);
        springView.setType(SpringView.Type.FOLLOW);
    }
    private void getData() {
        OkHttpUtils.post().url(Constant.ORDERWHOLEREQUESTPATH).addParams("phone", mPhone)
                .addParams("token", token).addParams("status", 3 + "").build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        showToast("连接不上服务器!");
                        springView.onFinishFreshAndLoad();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        springView.onFinishFreshAndLoad();
                        Log.e("--------",response);
                        if (response != null) {
                            //获取数据源
                            wholeOrderEntity = new Gson().fromJson(response, WholeOrderEntity.class);
                            String code = wholeOrderEntity.getCode().trim();
                            if (code.equals(200 + "")) {
                                data = wholeOrderEntity.getData();
                                if (data.size() > 0) {
                                    springView.setVisibility(View.VISIBLE);
                                    teNoOders.setVisibility(View.GONE);
                                    orderingList.clear();
                                    orderingList.addAll(data);
                                    //通知适配器数据改变
                                    orderingAdapter.notifyDataSetChanged();
                                } else {
                                    springView.setVisibility(View.GONE);
                                    teNoOders.setVisibility(View.VISIBLE);
                                }

                            } else {
                                springView.setVisibility(View.GONE);
                                teNoOders.setVisibility(View.VISIBLE);
                            }
                        } else {
                            springView.setVisibility(View.GONE);
                            teNoOders.setVisibility(View.VISIBLE);
                        }
                    }
                });
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(Ordering.this.getActivity().getApplication(), OrderDetailsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        bundle.putSerializable("wholeOrderEntity", wholeOrderEntity);
        intent.putExtra("bundle", bundle);
        intent.setAction("Ordering");
        startActivity(intent);
    }

    @Override
    public void onStart() {
        super.onStart();
        handlerOrdering.sendEmptyMessage(0);
    }

    @Override
    public void onStop() {
        super.onStop();
        handlerOrdering.removeMessages(0);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        handlerOrdering.removeMessages(0);
    }
}
